import os
import json
from datetime import datetime
from event import Event


if __name__ == "__main__":
    configData = None
    define = None
    kakoList = None

    # 获取 main.py 所在目录
    dir_directory = os.path.dirname(os.path.abspath(__file__))
    script_directory = os.path.dirname(dir_directory)
    # 构造 config.json 的路径
    config_file_path = os.path.join(script_directory, 'config.json')
    # 检查文件是否存在
    if os.path.exists(config_file_path):
        print(f"找到了配置文件: {config_file_path}")
    else:
        print("未找到 config.json 文件")
    try:
        # 打开并读取 JSON 文件
        with open(config_file_path, 'r', encoding='utf-8') as file:
            data = json.load(file)
            # 检查 JSON 数据中是否包含所需的键
            if "ConfigData" in data:
                configData = data["ConfigData"]
            else:
                print("Warning: 'ConfigData' key not found in the JSON file.")
            if "Define" in data:
                define = data["Define"]
            else:
                print("Warning: 'Define' key not found in the JSON file.")
            if "KakoList" in data:
                kakoList = data["KakoList"]
            else:
                print("Warning: 'KakoList' key not found in the JSON file.")
    except FileNotFoundError:
        print(f"Error: The file '{config_file_path}' was not found.")
    except json.JSONDecodeError:
        print("Error: The file contains invalid JSON.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

    try:
        event = Event(configData, define, kakoList, script_directory)
        event.init()
        event.add_message()
    except BaseException as e:
        print("运行报错： 时间", datetime.now(), "， 原因：", e)
